// page-deviceInformationImg/updateDeviceInfoImg/updateDeviceInfoImg.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.requestURL +'upload/uploadImgTest',
    path: '',//文件本地路径
    name: '',//name标识符
    imagesType: '',//自定义上传的目录路径
    identityNum: wx.getStorageSync('identification'),//身份证号
    update_zhengchesfz: wx.getStorageSync('update_zhengchesfz'), //7整车和身份证照
    update_namePlate: wx.getStorageSync('update_namePlate'), //13整车铭牌和身份证照
    update_dajiazisfz: wx.getStorageSync('update_dajiazisfz'), //14大架子（出厂编号）和身份证照
    update_engine: wx.getStorageSync('update_engine'), //15发动机铭牌照
    update_yeyabeng: wx.getStorageSync('update_yeyabeng'), //16液压泵铭牌照
    bg_identityNum: '',//身份证号
    bg_zhengchesfz: '', //7整车和身份证照
    bg_namePlate: '', //13整车铭牌和身份证照
    bg_dajiazisfz: '', //14大架子（出厂编号）和身份证照
    bg_engine: '', //15发动机铭牌照
    bg_yeyabeng: '', //16液压泵铭牌照
    bigShelfNum: '',
    qylxr: false,
    isSubmit: true,
    rollOutIdentification: ''//转出设备的选手
  },
  chooseImage: function (e) {
    console.log(e.target.id)
    var self = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: function (res) {
        if (e.target.id == '0') {
          self.setData({
            name: "zhengchesfz",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('update_zhengchesfz', imgResponseUrl)
            self.setData({
              update_zhengchesfz: wx.getStorageSync('update_zhengchesfz')
            })
            if (self.data.update_zhengchesfz != '') {
              self.setData({
                bg_zhengchesfz: ''
              })
            }
          };
        } else if (e.target.id == '1') {
          self.setData({
            name: "namePlate",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('update_namePlate', imgResponseUrl)
            self.setData({
              update_namePlate: wx.getStorageSync('update_namePlate')
            })
            if (self.data.update_namePlate != '') {
              self.setData({
                bg_namePlate: ''
              })
            }
          };
        } else if (e.target.id == '2') {
          self.setData({
            name: "dajiazisfz",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('update_dajiazisfz', imgResponseUrl)
            self.setData({
              update_dajiazisfz: wx.getStorageSync('update_dajiazisfz')
            })
            if (self.data.update_dajiazisfz != '') {
              self.setData({
                bg_dajiazisfz: ''
              })
            }
          };
        } else if (e.target.id == '3') {
          self.setData({
            name: "engine",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('update_engine', imgResponseUrl)
            self.setData({
              update_engine: wx.getStorageSync('update_engine')
            })
            if (self.data.update_engine != '') {
              self.setData({
                bg_engine: ''
              })
            }
          };
        } else if (e.target.id == '4') {
          self.setData({
            name: "yeyabeng",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('update_yeyabeng', imgResponseUrl)
            self.setData({
              update_yeyabeng: wx.getStorageSync('update_yeyabeng')
            })
            if (self.data.update_yeyabeng != '') {
              self.setData({
                bg_yeyabeng: ''
              })
            }
          };
        }
      }
    });
  },
  submit: function (e) {
    var that = this;
    if (that.data.isSubmit) {
      that.setData({
        isSubmit: false
      })
      var bigShelfNum = that.data.bigShelfNum;//设备编号
      var identityNum = that.data.identityNum;//身份证号
      var identityAndCarImg = that.data.update_zhengchesfz; //整车和身份证照
      if (identityAndCarImg == '') {
        identityAndCarImg = that.data.bg_zhengchesfz
      }
      var identityAndPlateImg = that.data.update_namePlate; //整车铭牌和身份证照
      if (identityAndPlateImg == '') {
        identityAndPlateImg = that.data.bg_namePlate
      }
      var identityAndBigShelfImg = that.data.update_dajiazisfz; //大架子号和身份证照
      if (identityAndBigShelfImg == '') {
        identityAndBigShelfImg == that.data.bg_dajiazisfz
      }
      var enginePlateImg = that.data.update_engine; //发动机铭牌照
      if (enginePlateImg == '') {
        enginePlateImg = that.data.bg_engine
      }
      var hydraulicPlateImg = that.data.update_yeyabeng;//液压泵铭牌照
      if (hydraulicPlateImg == null || hydraulicPlateImg == '') {
        hydraulicPlateImg = that.data.bg_yeyabeng
      }
      wx.request({
        url: app.globalData.requestURL+'deviceInformationImg/save',
        method: 'POST',
        data: {
          bigShelfNum: bigShelfNum,
          identityNum: identityNum,
          identityAndCarImg: identityAndCarImg,
          identityAndPlateImg: identityAndPlateImg,
          identityAndBigShelfImg: identityAndBigShelfImg,
          enginePlateImg: enginePlateImg,
          hydraulicPlateImg: hydraulicPlateImg
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.status == '0') {
            wx.setStorageSync('update_zhengchesfz', '');
            wx.setStorageSync('update_namePlate', '');
            wx.setStorageSync('update_dajiazisfz', '');
            wx.setStorageSync('update_engine', '');
            wx.setStorageSync('update_yeyabeng', '');
            wx.showToast({
              title: '添加成功！',
              icon: 'success',
              duration: 2000,
              mask: true,
              success: function () {
                wx.redirectTo({
                  url: '/page-deviceImg/updateDeviceImg/updateDeviceImg',
                })
              }
            });
          } else {
            wx.showToast({
              title: '提交失败！',
              icon: 'loading',
              duration: 2000,
              mask: true,
              success: function () {
                that.setData({
                  isSubmit: true
                })
              }
            });
          }
        }
      })
    } else {
      wx.showToast({
        title: '不可多次提交',
        icon: 'loading',
        duration: 2000,
        mask: true
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({
      bigShelfNum: wx.getStorageSync("rollOutFactoryNum"),//转出设备的编号
      identityNum: wx.getStorageSync('identification'),//转入单位
      rollOutIdentification: wx.getStorageSync("rollOutIdentification")//转出单位
    });
    var bigShelfNum = that.data.bigShelfNum
    var identityNum = that.data.identityNum
    var rollOutIdentification = that.data.rollOutIdentification

    //查询此照片表是否有提交过照片
    wx.request({
      url: app.globalData.requestURL+'deviceInformationImg/TbDeviceInformationImg',
      method: 'POST',
      data: {
        bigShelfNum: bigShelfNum,
        identityNum: identityNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == '1') { //如果转入人添加过就跳入下一页
          wx.redirectTo({
            url: '/page-deviceImg/updataDeviceImg/updataDeviceImg',
          })
        } if (res.data.status == '0') { //如果没添加过就用转出人的去查
          wx.request({
            url: app.globalData.requestURL +'deviceInformationImg/getNewDeviceInformationImgEntity',
            method: 'POST',
            data: {
              bigShelfNum: bigShelfNum,
              identityNum: rollOutIdentification
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res2) {
              if (res2.data.status == '0') {
                that.setData({
                  bg_zhengchesfz: res2.data.data[0].identityAndCarImg,//整车和身份证照
                  bg_namePlate: res2.data.data[0].identityAndPlateImg,//13整车铭牌和身份证照
                  bg_dajiazisfz: res2.data.data[0].identityAndBigShelfImg,//14大架子（出厂编号）和身份证照
                  bg_engine: res2.data.data[0].enginePlateImg,//15发动机铭牌照
                  bg_yeyabeng: res2.data.data[0].hydraulicPlateImg //16液压泵铭牌照
                }) 
              }
            }
          })
        }
      }
    })
    if (wx.getStorageSync("update_zhengchesfz") != '') {
      that.setData({
        bg_zhengchesfz: ''
      });
    }
    if (wx.getStorageSync("update_namePlate") != '') {
      that.setData({
        bg_namePlate: ''
      });
    }
    if (wx.getStorageSync("update_dajiazisfz") != '') {
      that.setData({
        bg_dajiazisfz: ''
      });
    }
    if (wx.getStorageSync("update_engine") != '') {
      that.setData({
        bg_engine: ''
      });
    }
    if (wx.getStorageSync("update_yeyabeng") != '') {
      that.setData({
        bg_yeyabeng: ''
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})